# coding=utf-8

import unittest
from lib import TestReport, sendmail, config_reader
import time
import os

def run(pattern,Subject):
    test_dir = r'/openmind-hub/auto_test/autotest/test_openmind_hub'
    suite = unittest.defaultTestLoader.discover(start_dir=test_dir, pattern=pattern)
    now = time.strftime('%Y-%m-%d_%H_%M_%S')
    reportname = r'/openmind-hub/auto_test/autotest/report/testreport/' + 'TestResult' + now + '.html'
    with open(reportname, 'wb') as f:
        runner = TestReport.HTMLTestReport(
            stream=f,
            title='测试报告',
            description='Test the import testcase',
            images=True
        )
        print("用例开始运行")
        runner.run(suite)
        print("用例运行全部结束")
    time.sleep(3)
    # 发送邮件
    mail = sendmail.SendMail()
    mail.send(config,Subject)


if __name__ == '__main__':
    print("读取config文件内容")
    global config
    config = config_reader.read_config()
    token_modelers = config.get("'DEFAULT'", "'token_modelers'")
    token_giteeAI = config.get("'DEFAULT'", "'token_giteeAI'")
    token_huggingface = config.get("'DEFAULT'", "'token_huggingface'")
    token_gitcode = config.get("'DEFAULT'", "'token_gitcode'")
    token_openi = config.get("'DEFAULT'", "'token_openi'")
    # os.remove(r"/vault/secrets/hub.ini")
    print("读取config文件内容完成")
    #"替换魔乐社区用例的token值"
    molecase_list=["/openmind-hub/auto_test/autotest/test_openmind_hub/test_modelers_model.py",
               "/openmind-hub/auto_test/autotest/test_openmind_hub/test_modelers_branch.py",
               "/openmind-hub/auto_test/autotest/test_openmind_hub/test_modelers_dataset.py",
               "/openmind-hub/auto_test/autotest/test_openmind_hub/test_modelers_space.py",
               "/openmind-hub/auto_test/autotest/test_openmind_hub/test_modelers_commit.py",
               "/openmind-hub/auto_test/autotest/test_openmind_hub/test_modelers_download.py",
               "/openmind-hub/auto_test/autotest/test_openmind_hub/test_modelers_omfilesystem.py"]
    for i in molecase_list:
        with open(i, 'r', encoding='utf-8') as f:
            f_content = f.read()
            f_content = f_content.replace('123456789()', token_modelers)
        with open(i, 'w', encoding='utf-8') as f:
            f.write(f_content)
    # "替换gitcode社区用例的token值"
    gitcodecase_list = ["/openmind-hub/auto_test/autotest/test_openmind_hub/test_thirdparty_gitcode.py"]
    for i in gitcodecase_list:
        with open(i, 'r', encoding='utf-8') as f:
            f_content = f.read()
            f_content = f_content.replace('123456789()', token_gitcode)
        with open(i, 'w', encoding='utf-8') as f:
            f.write(f_content)
    # "替换huggingface社区用例的token值"
    huggingfacecase_list = ["/openmind-hub/auto_test/autotest/test_openmind_hub/test_thirdparty_huggingface.py"]
    for i in huggingfacecase_list:
        with open(i, 'r', encoding='utf-8') as f:
            f_content = f.read()
            f_content = f_content.replace('123456789()', token_huggingface)
        with open(i, 'w', encoding='utf-8') as f:
            f.write(f_content)
    # "替换gitee AI社区用例的token值"
    giteeAIcase_list = ["/openmind-hub/auto_test/autotest/test_openmind_hub/test_thirdparty_giteeAI.py"]
    for i in giteeAIcase_list:
        with open(i, 'r', encoding='utf-8') as f:
            f_content = f.read()
            f_content = f_content.replace('123456789()', token_giteeAI)
        with open(i, 'w', encoding='utf-8') as f:
            f.write(f_content)
    openicase_list = ["/openmind-hub/auto_test/autotest/test_openmind_hub/test_thirdparty_openi.py"]
    for i in openicase_list:
        with open(i, 'r', encoding='utf-8') as f:
            f_content = f.read()
            f_content = f_content.replace('123456789()', token_openi)
        with open(i, 'w', encoding='utf-8') as f:
            f.write(f_content)

    os.environ["HUB_WHITE_LIST_PATHS"] = "/openmind-hub"
    os.environ["OPENMIND_HUB_ENDPOINT"]  = "https://openmind.test.osinfra.cn"
    run("test_modelers*.py", "魔乐社区工具链测试报告")




    # # 根据不同社区执行用例并发送邮件
    # pattern_list = ["test_modelers_branch.py","test_thirdparty_gitcode.py","test_thirdparty_giteeAI.py","test_thirdparty_huggingface.py"]
    # for i in pattern_list:
    #     if i == "test_modelers_branch.py":
    #         # 添加环境变量
    #         os.environ["HUB_WHITE_LIST_PATHS"] = "/openmind-hub"
    #         run(i, "魔乐社区工具链测试报告")
    #     if i == "test_thirdparty_gitcode.py":
    #         # 添加环境变量
    #         os.environ["HUB_WHITE_LIST_PATHS"] = "/openmind-hub"
    #         os.environ["OPENMIND_HUB_ENDPOINT"] = "https://api.gitcode.com"
    #         run(i, "gitcode社区工具链测试报告")
    #     if i == "test_thirdparty_giteeAI.py":
    #         # 添加环境变量
    #         os.environ["HUB_WHITE_LIST_PATHS"] = "/openmind-hub"
    #         os.environ["HF_ENDPOINT"] = "https://hf-api.gitee.com"
    #         set_platform("gitee")
    #         run(i, "giteeAI社区工具链测试报告")
    #     if i == "test_thirdparty_huggingface.py":
    #         # 添加环境变量
    #         os.environ["HUB_WHITE_LIST_PATHS"] = "/openmind-hub"
    #         set_platform("huggingface")
    #         run(i, "huggingface社区工具链测试报告")
